package com.Questions.DFS;


import java.util.*;

/**
 * 对称二叉树
 */
public class leetcode101 {
    public boolean isSymmetric1(TreeNode root) {
        if(root==null){
            return true;
        }
        return deepCheck(root.left,root.right);
    }
    public boolean deepCheck(TreeNode left,TreeNode right){
        if(left==null&&right==null){
            return true;
        }
        if(left==null||right==null){
            return false;
        }
        if(left.val!=right.val){
            return false;
        }
        return deepCheck(left.left,right.right)&&deepCheck(left.right,right.left);
    }
    public boolean isSymmetric(TreeNode root){
        Queue<TreeNode> q=new LinkedList<>();
        // 使用额外的存储空间
        TreeNode u=root.left;
        TreeNode v=root.right;
        if(root==null||(u==null&&v==null)){
            return true;
        }
        q.offer(u);
        q.offer(v);
        while (!q.isEmpty()){
            u=q.poll();//获取尾元素并删除
            v=q.poll();
            if(u==null&&v==null){
                continue;
            }
            if((u==null||v==null)||(u.val!=v.val)){
                return false;
            }
            q.offer(u.left);
            q.offer(v.right);

            q.offer(u.right);
            q.offer(v.left);
        }
        return true;
    }
    // 还可以中序遍历 然后判断得到的字符串是不是回文
}
